//
//  YYSRecommendController.m
//  FanLaiWu
//
//  Created by gonglei on 17/3/15.
//  Copyright © 2017年 yys. All rights reserved.
//

#import "YYSRecommendController.h"
#import "YYSRecomandCell.h"
#import "XRCarouselView.h"
#import "YYSZiXunCell.h"
#import "YYSDetailNewsVideoController.h"
#import "YYSZiXunController.h"
#import "YYSHomeListModel.h"
@interface YYSRecommendController () <UITableViewDelegate,UITableViewDataSource,XRCarouselViewDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomHeiht;
@property (strong, nonatomic) XRCarouselView *carouselView;
@property (nonatomic , assign) ControllerType type;
@property (nonatomic , strong) NSMutableArray <Column *>*columModels;
@property (nonatomic , strong) NSMutableArray <News *>*newsModels;
@property (nonatomic , strong) NSMutableArray <Ad *>*adModels;
@end

@implementation YYSRecommendController
static NSString *ID = @"YYSRecomandCell";
static NSString *ID2 = @"YYSZiXunCell";
-(NSMutableArray <Column *>*)columModels
{
    if (_columModels == nil) {
        _columModels = [NSMutableArray array];
    }
    return _columModels;
}
-(NSMutableArray<News *> *)newsModels
{
    if (_newsModels == nil) {
        _newsModels = [NSMutableArray array];
    }
    return _newsModels;
}
-(NSMutableArray<Ad *> *)adModels
{
    if (_adModels == nil) {
        _adModels = [NSMutableArray array];
    }
    
    return _adModels;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.tableView registerNib:[UINib nibWithNibName:ID bundle:nil] forCellReuseIdentifier:ID];
    [self.tableView registerNib:[UINib nibWithNibName:ID2 bundle:nil] forCellReuseIdentifier:ID2];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, -170);
    [self setUI];
    if (YYSScreenH <= 600) {
        self.bottomHeiht.constant = 165;
    } else  if (YYSScreenH >=700){
        self.bottomHeiht.constant = 175;
    }
    self.type = RecommandType;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"id"] = @1;
//    dict[@"password"] = _passwordText.text;
    [NSObject POST:HOME_LIST_URL parameters:dict progress:^(NSProgress *downloadProgress) {
        
    } completionHandler:^(id responseObj, NSError *error) {
        NSLog(@"%@",responseObj);
       
        if ([responseObj[@"errno"] integerValue] == 0) {
           
            
            
            for (NSDictionary *dict in responseObj[@"result"][@"ad"]) {
//                [self.adModels addObject:[Ad modelWithJSON:dict]];
                Ad *model = [Ad modelWithJSON:dict];
                model.ID = dict[@"id"];
                [self.adModels addObject:model];
            }
            for (NSDictionary *dict in responseObj[@"result"][@"column"]) {
//                [self.columModels addObject:[Column modelWithJSON:dict]];
                Column *model = [Column modelWithJSON:dict];
                model.ID = dict[@"id"];
                [self.columModels addObject:model];
            }
            for (NSDictionary *dict in responseObj[@"result"][@"news"]) {
//                [self.newsModels addObject:[News modelWithJSON:dict]];
                News *model = [News modelWithJSON:dict];
                model.ID = dict[@"id"];
                [self.newsModels addObject:model];

            }
            
            [self.tableView reloadData];
            
        } else {
            [SVProgressHUD showErrorWithStatus:responseObj[@"errmsg"]];
        }
    }];
}
- (void)setUI
{
    
}
-(void)carouselView:(XRCarouselView *)carouselView clickImageAtIndex:(NSInteger)index
{
//    if (index == 0) {
        YYSDetailNewsVideoController *news = [[YYSDetailNewsVideoController alloc] init];
        news.ID = [self.adModels[index].ID integerValue];
        [self.view.superview.viewController.navigationController pushViewController:news animated:YES];
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    int i = 0;
    
    if (_newsModels.count != 0) {
        i++;
    }
    
    return i + 1 + _columModels.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else if (section > _columModels.count){
        return _columModels.count;
    } else {
        return 1;
    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, YYSScreenW, 150)];
        
        NSMutableArray *imageArr = [NSMutableArray array];
        for (Ad *ad in _adModels) {
//            UIImage *image = [UIImage ];
            [imageArr addObject:[NSString stringWithFormat:@"%@%@",BASE_URL_TEST,ad.cover]];
        }
        
        //设置图片及描述数组
        self.carouselView = [XRCarouselView new];
        self.carouselView.imageArray = imageArr;
        //    self.carouselView.describeArray = describeArray;
        
        //用代理处理图片点击
        self.carouselView.delegate = self;
        
        //设置每张图片的停留时间，默认值为5s，最少为2s
        _carouselView.time = 2;
        
        //设置分页控件的图片,不设置则为系统默认
        [_carouselView setPageImage:[UIImage imageNamed:@"other"] andCurrentPageImage:[UIImage imageNamed:@"current"]];
        
        //设置分页控件的位置，默认为PositionBottomCenter
        _carouselView.pagePosition = PositionBottomCenter;
        
        /**
         *  修改图片描述控件的外观，不需要修改的传nil
         *
         *  参数一 字体颜色，默认为白色
         *  参数二 字体，默认为13号字体
         *  参数三 背景颜色，默认为黑色半透明
         */
        UIColor *bgColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        UIFont *font = [UIFont systemFontOfSize:15];
        UIColor *textColor = [UIColor greenColor];
        [_carouselView setDescribeTextColor:textColor font:font bgColor:bgColor];
        _carouselView.frame = CGRectMake(10, 0, YYSScreenW - 20, 130);
        UIView *bottomV = [[UIView alloc] initWithFrame:CGRectMake(0, 140, YYSScreenW, 10)];
        bottomV.backgroundColor = YYSRGBColor(235, 235, 235);
        [view addSubview:bottomV];
        [view addSubview:_carouselView];
        return view;
    } else if (section == 1 || section == 2|| section == 3) {
        UIView *view = [UIView new];
        view.frame = CGRectMake(0, 0, YYSScreenW, 40);
        UIImageView *imageV = [[UIImageView alloc] init];
        if (section == 1 || section == 2) {
            [imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASE_URL_TEST,self.columModels[section-1].icon]]];
        } else {
            imageV.image = [UIImage imageNamed:@"zixun"];
        }
        
        /*
        if (section == 1) {
//            [imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASE_URL_TEST,self.listModels[section].esArray]]];
//            NSString *str = self.models[section][@"icon"];
            
        } else if (section == 2) {
            imageV.image = [UIImage imageNamed:@"xinxianshipin"];
        }else{
            imageV.image = [UIImage imageNamed:@"zixun"];
        }
        */
        imageV.frame = CGRectMake(10, 10, 20, 20);
        [view addSubview:imageV];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, 100, 20)];
        
//        label.tag = section;
        if (section == 1) {
//            label.text = @"新闻资讯";
            label.text = self.columModels[section-1].name;
            view.tag = 1;
        } else if (section == 2){
            view.tag = 2;
            label.text = self.columModels[section-1].name;
//            label.text = @"新鲜视频";
        } else {
            view.tag = 3;
            label.text = @"资讯";
        }
        label.textColor = YYSLabelTextColor;
        label.font = [UIFont systemFontOfSize:15];
        [view addSubview:label];
        
        UIImageView *imageV2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navright"]];
        imageV2.frame = CGRectMake(YYSScreenW - 40, 10, 20, 20);
        [view addSubview:imageV2];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(YYSScreenW - 160, 12, 120, 18)];
        label2.text = @"更多精彩";
        
        
        label2.textColor = YYSLabelTextColor;
        label2.userInteractionEnabled = YES;
        label2.textAlignment = NSTextAlignmentRight;
        label2.font = [UIFont systemFontOfSize:15];
        [view addSubview:label2];
        UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 38, YYSScreenW, 2)];
        bottomView.backgroundColor = YYSRGBColor(235, 235, 235);
        [view addSubview:bottomView];
        [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickMore:)]];
        return view;
    } else {
        return nil;
    }
}
- (void)clickMore:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 1) {
        YYSDetailNewsVideoController *news = [[YYSDetailNewsVideoController alloc] init];
        news.title = self.columModels[tap.view.tag - 1].name;
        news.ID = [self.columModels[tap.view.tag - 1].ID integerValue];
        [self.view.superview.viewController.navigationController pushViewController:news animated:YES];
    } else if (tap.view.tag == 2) {
        YYSDetailNewsVideoController *news = [[YYSDetailNewsVideoController alloc] init];
        news.title = self.columModels[tap.view.tag - 1].name;
        news.ID = [self.columModels[tap.view.tag - 1].ID integerValue];
        [self.view.superview.viewController.navigationController pushViewController:news animated:YES];
    } else {
        YYSZiXunController *zixu = [[YYSZiXunController alloc] init];
        zixu.title = @"资讯";
        
        [self.view.superview.viewController.navigationController pushViewController:zixu animated:YES];
        
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 130+20;
    } else {
        return 40;
    }
//    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 || indexPath.section == 2) {
        return 330;
    } else {
        return 100;
    }
//    return 330;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 1 || indexPath.section == 2) {
        YYSRecomandCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        cell.type = self.type;
        cell.model = self.columModels[indexPath.section-1].video;
        return cell;
    } else {
        YYSZiXunCell *cell = [tableView dequeueReusableCellWithIdentifier:ID2];
        cell.news = self.newsModels[indexPath.row];
        return cell;
    }
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
//        YYSDetailNewsVideoController *detail = [[YYSDetailNewsVideoController alloc] init];
//        [self.navigationController pushViewController:detail animated:YES];
    } else {
        
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
    //让头部视图随着滚动消失
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 220;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
